Title: SQL BT (By-BKK) / 10 Aggregates
Tags: Obsidian_to_Anki

Q: What is an aggregate function in SQL?
A: A function that summarizes data over a group of rows

Q: Name five common aggregate functions in SQL.
A: COUNT, MIN, MAX, AVG, SUM

Q: How does COUNT(_) differ from COUNT(column)?
A: COUNT(_) counts all rows, COUNT(column) ignores NULL values

Q: What does GROUP BY do in SQL?
A: Groups rows with the same value to apply aggregate functions to each group

Q: True or False: Aggregate functions can run without grouping to summarize all rows.
A: True

Q: How do you give an aggregate function a readable name in results?
A: Use AS to assign an alias

Q: Write a query to find the average years employed for each role.
A: SELECT role, AVG(years_employed) AS avg_years FROM employees GROUP BY role

Q: How do you find the total years worked in each building using SQL?
A: SELECT building, SUM(years_employed) AS total_years FROM employees GROUP BY building

Q: Write a query to find the longest employment time in the studio.
A: SELECT MAX(years_employed) AS longest_employment FROM employees

Q: Reverse card: A function that summarizes data over rows
A: What is an aggregate function in SQL?

Q: Reverse card: GROUP BY
A: Which SQL clause creates distinct groups for aggregate calculations?

Q: Reverse card: COUNT, MIN, MAX, AVG, SUM
A: Name five common SQL aggregate functions

Q: Reverse card: True
A: Can aggregate functions run over all rows without GROUP BY?

Q: Reverse card: AS
A: Which SQL keyword assigns an alias to an aggregate function?

Q: Reverse card: SELECT building, SUM(years_employed) AS total_years FROM employees GROUP BY building
A: How do you find the total employee years for each building?

Q: Reverse card: SELECT MAX(years_employed) AS longest_employment FROM employees
A: What query finds the longest time any employee has worked at the studio?